# 下载与解压文件
# 创建人：曾逸夫
# 创建时间：2022-06-06

import os
from pathlib import Path
from zipfile import ZipFile

import torch

file_download_path = "https://gitee.com/CV_Lab/gradio_yolov5_det/attach_files/1085076/download/coco128.zip"
file = "coco128.zip"


def unzip_file(file):
    # 参考：https://github.com/ultralytics/yolov5/blob/master/utils/downloads.py
    file = Path(file)
    if file.suffix == '.zip':
        print('unzipping... ', end='')
        ZipFile(file).extractall(path=file.parent)  # unzip
        file.unlink()  # remove zip


def file_download_unzip(file_download_path, file):
    file_exit = os.path.exists(file)
    if not file_exit:
        torch.hub.download_url_to_file(file_download_path, file)
        unzip_file(file)

    else:
        print(f"{file}已存在")
        unzip_file(file)


if __name__ == '__main__':
    file_download_unzip(file_download_path, file)
